/*
Copyright (c) 2014 Sanjay Dasgupta (sanjay.dasgupta@gmail.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package ipex

import scala.swing._
import scala.util.{Try, Success, Failure}

object AreaMap extends GridPanel(13, 1) {

  def setLabel(label: String) {
    Main.clearSearchText()
    Try(MemData.labelMap(label)) match {
      case Success(p) =>
        setResults(p._1, p._2)
        Main.title(s"'$label'")
        Main.searchSuccessful()
        Main.displayForms(Nil)
      case Failure(_) =>
        Dialog.showMessage(Main.top.contents.head, s"Area '$label' is still incomplete")
    }
  }

  val abandonAndWithdraw = Button("<html><div align='center'>Abandonment<br/>&amp; Withdrawal</div></html>") {
    setLabel("Abandonment & Withdrawal")}
  val addition = Button("<html><div align='center'>Patents<br/>of Addition</div></html>"){
    setLabel("Patents of Addition")}
  val amendmentPost = Button("<html><div align='center'>Post-Grant<br/>Amendment</div></html>") {
    setLabel("Post-Grant Amendment")}
  val amendmentPre = Button("<html><div align='center'>Pre-Grant<br/>Amendment</div></html>") {
    setLabel("Pre-Grant Amendment")}
  val basics = Button("Basics & Patentability") {setLabel("Basics & Patentability")}
  val application = Button("Application") {setLabel("Application")}
  val compulsoryLicensing = Button("<html><div align='center'>Compulsory<br/>Licensing</div></html>") {
    setLabel("Compulsory Licensing")
  }
  val examination = Button("Examination") {setLabel("Examination")}
  val grant = Button("Grant") {setLabel("Grant")}
  val infringement = Button("Infringement"){setLabel("Infringement")}
  val oppositionPreGrant = Button("<html><div align='center'>Pre-Grant<br/>Opposition</div></html>") {
    setLabel("Pre-Grant Opposition")}
  val oppositionAndAmendmentPre = new GridPanel(1, 2) {
    contents += (oppositionPreGrant, amendmentPre)
  }
  val oppositionPostGrant = Button("<html><div align='center'>Post-Grant<br/>Opposition</div></html>") {
    setLabel("Post-Grant Opposition")}
  val oppositionAndAmendmentPost = new GridPanel(1, 2) {
    contents += (oppositionPostGrant, amendmentPost)
  }
  val patentAgents = Button("<html><div align='center'>Patent<br/>Agents</div></html>"){
    setLabel("Patent Agents")}
  val patentOffice = Button("<html><div align='center'>Patent<br/>Office</div></html>"){
    setLabel("Patent Office")}
  val penalty = Button("<html><div align='center'>Penalty<br/>&amp; Liability</div></html>"){
    setLabel("Penalty & Liability")}
  val infringementAndPenalty = new GridPanel(1, 2) {contents += (infringement, penalty)}
  val publication = Button("<html><div align='center'>Publication<br/>&amp; Secrecy</div></html>") {
    setLabel("Publication & Secrecy")}
  val publicationAndExamination = new GridPanel(1, 2) {contents += (publication, examination)}
  val revocation = Button("Revocation"){setLabel("Revocation")}
  val compulsoryAndRevocation = new GridPanel(1, 2) {contents += (compulsoryLicensing, revocation)}
  val renewal = Button("<html><div align='center'>Renewal, Lapse<br/>&amp; Restoration</div></html>"){
    setLabel("Renewal, Lapse & Restoration")}
  val register = Button("<html><div align='center'>Register<br/>of Patents</div></html>"){
    setLabel("Register of Patents")}
  val additionAndRegister = new GridPanel(1, 2) {contents += (addition, register)}
  val surrender = Button("Surrender"){setLabel("Surrender")}
  val specification = Button("Specification") {setLabel("Specification")}
  val applicationAndSpecification = new GridPanel(1, 2) {contents += (application, specification)}
  val dating = Button("<html><div align='center'>Priority<br/>&amp; Dating</div></html>") {
    setLabel("Priority & Dating")}
  val patentAgentsAndOffice = new GridPanel(1, 2) {contents += (patentAgents, patentOffice)}
  val abandonWithdrawDate = new GridPanel(1, 2) {contents += (abandonAndWithdraw, dating)}
  contents += (basics, applicationAndSpecification, abandonWithdrawDate, publicationAndExamination,
    oppositionAndAmendmentPre, grant, oppositionAndAmendmentPost, additionAndRegister, infringementAndPenalty,
    compulsoryAndRevocation, renewal, surrender, patentAgentsAndOffice)

  def setResults(sectNos: Seq[String], ruleNos: Seq[String]) {
    def displayString(isRule: Boolean, nbr: String): String = {
      (isRule, nbr.startsWith("*")) match {
        case (true, true) => val nbr2 = nbr.substring(1)
          s"<html><font color='#000000'>$nbr2. ${MemData.ruleTitles(nbr2)}</font></html>"
        case (true, false) => s"<html><font color='#888888'>$nbr. ${MemData.ruleTitles(nbr)}</font></html>"
        case (false, true) =>  val nbr2 = nbr.substring(1)
          s"<html><font color='#000000'>$nbr2. ${MemData.sectionTitles(nbr2)}</font></html>"
        case (false, false) => s"<html><font color='#888888'>$nbr. ${MemData.sectionTitles(nbr)}</font></html>"
      }
    }
    Main.actResultsList.listData = sectNos.map(displayString(false, _))
    Main.rulesResultsList.listData = ruleNos.map(displayString(true, _))
  }
}
